<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Breview_model extends Model
{
	private $_host;
	private $_dbname;
	private $_username;
	private $_password;
	private $_port;
	private $_folderImageStore = 'uploads/images/';
	
	function Breview_model()
	{
		parent::Model();
		
		$host = $this->db->hostname;
		$arrHost = explode(':',$host);
		
		$this->_host = trim($arrHost[0]);
		$this->_dbname = $this->db->database;
		$this->_username = $this->db->username;
		$this->_password = $this->db->password;
		$this->_port = $this->db->port;
	}
	
	function list_banner_processing($uid, $filter = 'all')
	{
		$data = array();
		
		switch($filter)
		{
			case 'new': $filter = 0; break;
			case 'old': $filter = 1; break;
			case 'exi': $filter = 2; break;
			default: $filter = -1; break;
		}

		$iconn = mysqli_connect($this->_host, $this->_username, $this->_password, $this->_dbname, $this->_port);
		$result = mysqli_query($iconn, "CALL selfserving_get_all_banner_processing($uid,$filter);") or die(mysqli_error());
		while($row = mysqli_fetch_array($result))
		{
			$row['activate'] = date('d-m-Y', strtotime($row['activate']));
			$row['expire'] = date('d-m-Y', strtotime($row['expire']));
			$row['filepath'] = '';
			if(file_exists($this->_folderImageStore . $row['campaignid'] . '/' . $row['filename']))
			{
				$row['filepath'] = '/' . $this->_folderImageStore . $row['campaignid'] . '/' . $row['filename'];
			}
			$data[] = $row;
		}
		mysqli_free_result($result);
		mysqli_close($iconn);
		return $data;
	}
	
	function list_banner_processing2($uid, $filter = 'all', $page=1,$limit=20)
	{
		$data = array();
		
		switch($filter)
		{
			case 'new': $filter = 0; break;
			case 'old': $filter = 1; break;
			case 'exi': $filter = 2; break;
			default: $filter = -1; break;
		}

		$iconn = mysqli_connect($this->_host, $this->_username, $this->_password, $this->_dbname, $this->_port);
		$result = mysqli_query($iconn, "CALL selfserving_get_all_banner_processing($uid,$filter,'',$page,$limit,-1);") or die(mysqli_error());
		while($row = mysqli_fetch_array($result))
		{
			$row['activate'] = date('d-m-Y', strtotime($row['activate']));
			$row['expire'] = date('d-m-Y', strtotime($row['expire']));
			$row['filepath'] = '';
			if(file_exists($this->_folderImageStore . $row['campaignid'] . '/' . $row['filename']))
			{
				$row['filepath'] = '/' . $this->_folderImageStore . $row['campaignid'] . '/' . $row['filename'];
			}
			
			$data[] = $row;
		}
		
		mysqli_next_result($iconn);
        $result = mysqli_store_result($iconn);
        $total_row = mysqli_fetch_array($result);
        
        mysqli_next_result($iconn);
        $result = mysqli_store_result($iconn);
        $total_sum = array();
		while($row = mysqli_fetch_array($result))
		{
			$total_sum[] = $row;
		}
		
		$return = array();
		$return['bList'] = $data;
		$return['total_page'] = $total_row[0];
		$return['status'] = $total_sum;
		
		mysqli_free_result($result);
		mysqli_close($iconn);
		return $return;
	}
	
	function get_sum_status_banner($uid)
	{
		$iconn = mysqli_connect($this->_host, $this->_username, $this->_password, $this->_dbname, $this->_port);
		$result = mysqli_query($iconn, "CALL selfserving_get_sum_status_banner($uid);") or die(mysqli_error());
		$row = mysqli_fetch_array($result);
		
		mysqli_free_result($result);
		mysqli_close($iconn);
		
		return $row;
	}
	
	function get_banner_info($bannerid)
	{
		$data = array();
        $data['bInfo'] = array();
        $data['lstCat'] = array();
		$iconn = mysqli_connect($this->_host, $this->_username, $this->_password, $this->_dbname, $this->_port);
		mysqli_multi_query($iconn, "CALL selfserving_get_banner_info('$bannerid');") or die(mysqli_error());
        $result = mysqli_store_result($iconn);
		while($row = mysqli_fetch_array($result))
		{
			$row['filepath'] = '';
			
			$createdTime = date($row['createtime']);
			$createdTimeInfo = getdate(strtotime($createdTime));
			$yearFolder = $createdTimeInfo['year'];
			$monthFolder = $createdTimeInfo['mon'];
			$monthFolder = ($monthFolder < 10) ? "0" . $monthFolder : $monthFolder;
			
			if(file_exists($this->_folderImageStore . $yearFolder . '/' . $monthFolder . '/' . $row['filename']))
			{
				$row['filepath'] = '/' . $this->_folderImageStore . $yearFolder . '/' . $monthFolder . '/' . $row['filename'];
			}
			$data['bInfo'] = $row;
		}
        
        mysqli_next_result($iconn);
        $result = mysqli_store_result($iconn);
        while($row = mysqli_fetch_array($result))
		{
			$data['lstCat'][] = $row;
		}
                
		mysqli_free_result($result);
		mysqli_close($iconn);
		return $data;
        
		return $data;
	}
	
	function get_user_info($userid)
	{
		$data = array();
		$iconn = mysqli_connect($this->_host, $this->_username, $this->_password, $this->_dbname, $this->_port);
		$result = mysqli_query($iconn, "CALL selfserving_get_user_info('$userid');") or die(mysqli_error());
		while($row = mysqli_fetch_array($result))
		{
			$data = $row;
		}
		mysqli_free_result($result);
		mysqli_close($iconn);
		return $data;
	}
	
	function publisher_list()
	{
		$data = array();
		$iconn = mysqli_connect($this->_host, $this->_username, $this->_password, $this->_dbname, $this->_port);
		$result = mysqli_query($iconn, "CALL selfserving_users_list_publisher();") or die(mysqli_error());
		while($row = mysqli_fetch_array($result))
		{
			$data[] = $row;
		}
		mysqli_free_result($result);
		mysqli_close($iconn);
		return $data;
	}
	
	function publisher_change($bannerIds, $uid)
	{
		$iconn = mysqli_connect($this->_host, $this->_username, $this->_password, $this->_dbname, $this->_port);
		$result = mysqli_query($iconn, "CALL selfserving_users_change_publisher('$bannerIds', $uid);") or die(mysqli_error());
		mysqli_close($iconn);
	}
	
	function apply_banner_log_mail($bannerid, $publisherid, $send, $receive, $cc, $subject, $content, $type, $note='')
	{
		$iconn = mysqli_connect($this->_host, $this->_username, $this->_password, $this->_dbname, $this->_port);
		$result = mysqli_query($iconn, "CALL selfserving_notify_email_log('$bannerid', '$publisherid', '$send', '$receive', '$cc', '$subject', '$content', '$type', '$note');") or die(mysqli_error());
		mysqli_close($iconn);
	}
	
	function apply_banner($bannerid, $uid=0)
	{
		if($uid == 0) $uid = (int)$this->session->userdata('uid');
		$iconn = mysqli_connect($this->_host, $this->_username, $this->_password, $this->_dbname, $this->_port);
		$result = mysqli_query($iconn, "CALL selfserving_banner_approved('$bannerid',$uid);") or die(mysqli_error());
		mysqli_close($iconn);
	}
	
	function get_campaign_info($campid)
	{
		$data = array();
		$iconn = mysqli_connect($this->_host, $this->_username, $this->_password, $this->_dbname, $this->_port);
		$result = mysqli_query($iconn, "CALL selfserving_get_campaign_info('$campid');") or die(mysqli_error());
		while($row = mysqli_fetch_array($result))
		{
			$data = $row;
		}
		mysqli_free_result($result);
		mysqli_close($iconn);
		return $data;
	}
}